package com.fiume.travel.service.impl;

import com.fiume.travel.constant.RedisConstant;
import com.fiume.travel.mapper.RouteMapper;
import com.fiume.travel.pojo.Route;
import com.fiume.travel.pojo.RouteExample;
import com.fiume.travel.service.AffixService;
import com.fiume.travel.service.RouteService;
import com.fiume.travel.utils.BeanConv;
import com.fiume.travel.utils.EmptyUtil;
import com.fiume.travel.vo.AffixVo;
import com.fiume.travel.vo.RouteVo;
import com.fiume.travel.vo.SellerVo;
import com.fiume.travel.vo.UserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * Description: 线路模块实现类
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-02-21 23:07
 * @see com.fiume.travel.service.impl
 */
@Service
public class RouteServiceImpl implements RouteService {

    @Resource
    private RouteMapper routeMapper;

    @Resource
    private HttpSession httpSession;

    @Resource
    private RedissonClient redissonClient;

    @Resource
    private AffixService affixService;
    /**
     * 添加路线
     *
     * @param routeVo 路径请求参数
     * @return 影响行数
     */
    @Override
    public Integer addRoute(RouteVo routeVo) {
        /*TODO: 测试的时候发现商家模块暂时没写,用用户模块代替了商家模块
        //1. 获取当前用户
        //从session中取出用户对象
        SellerVo sellerVo = (SellerVo) httpSession.getAttribute(httpSession.getId());
        //判断用户是否存在
        if (EmptyUtil.isNullOrEmpty(sellerVo)){
            return null;
        }
        //将属性整合到vo对象中
        routeVo.setSellerId(sellerVo.getId());
        //插入并返回
        return routeMapper.insert(BeanConv.toBean(routeVo, Route.class));*/


        //1. 获取当前用户
        //从session中取出用户对象
        UserVo userVo = (UserVo) httpSession.getAttribute(httpSession.getId());
        //判断用户是否存在
        if (EmptyUtil.isNullOrEmpty(userVo)){
            return null;
        }
        //将属性整合到vo对象中 TODO: 后期改回商家模块
        routeVo.setSellerId(userVo.getId());
        //插入并返回
        return routeMapper.insert(BeanConv.toBean(routeVo, Route.class));
    }

    /**
     * 更新线路
     *
     * @param routeVo 线路请求参数
     * @return 影响行数
     */
    @Override
    public Integer updateRoute(RouteVo routeVo) {
        //1. 获取当前用户
        //从session中取出用户对象 TODO: 后期改
//        SellerVo sellerVo = (SellerVo) httpSession.getAttribute(httpSession.getId());
        UserVo sellerVo = (UserVo) httpSession.getAttribute(httpSession.getId());

        //判断用户是否存在
        if (EmptyUtil.isNullOrEmpty(sellerVo)){
            return null;
        }
        //补全vo数据
        routeVo.setSellerId(sellerVo.getId());
        //获取需要更新的pojo对象
        Route route = BeanConv.toBean(routeVo, Route.class);
        int result = routeMapper.updateByPrimaryKeySelective(route);
        if (result>0){
            //删除缓存的线路信息
            RBucket<RouteVo> bucket = redissonClient.getBucket(RedisConstant.ROUTESERVICE_FINDROUTEBYID + routeVo.getId());
            bucket.delete();
            //删除缓存的图片信息
            RBucket<RouteVo> bucket1 = redissonClient.getBucket(RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID + routeVo.getId());
            bucket1.delete();
        }
        return result;
    }

    /**
     * 根据id查询线路详情
     *
     * @param routeVo 路径请求参数
     * @return 线路详情
     */
    @Override
    public RouteVo findRouteById(RouteVo routeVo) {
        //1. 获取当前用户
        //从session中取出用户对象, TODO: 后期改回商家模块
//        SellerVo sellerVo = (SellerVo) httpSession.getAttribute(httpSession.getId());
        /*UserVo sellerVo = (UserVo) httpSession.getAttribute(httpSession.getId());
        //判断用户是否存在
        if (EmptyUtil.isNullOrEmpty(sellerVo)){
            return null;
        }*/

        //读取缓存信息
        RBucket<RouteVo> bucket = redissonClient
                .getBucket(RedisConstant.ROUTESERVICE_FINDROUTEBYID + routeVo.getId());
        RouteVo routeVoCache = bucket.get();
        if (!EmptyUtil.isNullOrEmpty(routeVoCache)){
            return routeVoCache;
        }
        //如果缓存中没有线路详情信息,根据id在mysql中查询
        Route route = routeMapper.selectByPrimaryKey(routeVo.getId());
        RouteVo resultRouteVo = BeanConv.toBean(route, RouteVo.class);

        if (!EmptyUtil.isNullOrEmpty(resultRouteVo)){
            //查询当前线路对应的图片信息
            List<AffixVo> affixByBusinessId = affixService.findAffixByBusinessId(
                    AffixVo.builder()
                            .businessId(routeVo.getId())
                            .build());
            resultRouteVo.setAffixVoList(affixByBusinessId);
            //将结果放入缓存中
            bucket.set(resultRouteVo,1800, TimeUnit.SECONDS);
        }
        //返回数据
//        return BeanConv.toBean(route, RouteVo.class);
        return resultRouteVo;
    }

    /**
     * 分页查询线路
     *
     * @param routeVo  路径请求参数
     * @param pageNum  当前页数
     * @param pageSize 分页大小
     * @return 线路集合
     */
    @Override
    public PageInfo<RouteVo> findRouteByPage(RouteVo routeVo, int pageNum, int pageSize) {
        // 将pageNum和pageSize存放到当前线程上
        PageHelper.startPage(pageNum,pageSize);
        // 拼接查询条件
        RouteExample example = new RouteExample();
        RouteExample.Criteria criteria = example.createCriteria();
        //多条件查询
        // 根据分类id查询分类下的线路
        if (!EmptyUtil.isNullOrEmpty(routeVo.getCategoryId())){
            criteria.andCategoryIdEqualTo(routeVo.getCategoryId());
        }
        // 根据线路名称模糊查询
        if (!EmptyUtil.isNullOrEmpty(routeVo.getRouteName())){
            criteria.andRouteNameLike("%"+routeVo.getRouteName()+"%");
        }
        // 根据最低价格模糊查询
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMinPrice())){
            criteria.andPriceGreaterThan(routeVo.getMinPrice());
        }
        // 根据最高价格模糊查询
        if (!EmptyUtil.isNullOrEmpty(routeVo.getMaxPrice())){
            criteria.andPriceLessThan(routeVo.getMaxPrice());
        }
        // 首页上 人气旅游 主题旅游 最新旅游 条件
        // 最新旅游
        if (!EmptyUtil.isNullOrEmpty(routeVo.getOrderNewest())){
            example.setOrderByClause("created_time desc");
        }
        // 人气旅游
        if (!EmptyUtil.isNullOrEmpty(routeVo.getOrderPopularity())){
            example.setOrderByClause("attention_count asc");
        }
        List<Route> routeList = routeMapper.selectByExample(example);
        // 立即封装查询结果
        PageInfo<Route> pageInfo = new PageInfo<>(routeList);
        // 定义返回值pageInfo
        PageInfo<RouteVo> resultPageInfo = new PageInfo<>();
        // 拷贝基本类型数据
        BeanConv.toBean(pageInfo,resultPageInfo);
        // 拷贝list集合数据
        List<RouteVo> routeVoList = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);

        resultPageInfo.setList(routeVoList);

        //遍历查询到的线路集合,根据线路id查询线路对应的图片信息
        for (RouteVo routeVoHandler : routeVoList) {
            AffixVo affixVo = AffixVo.builder()
                                        .businessId(routeVoHandler.getId())
                                        .build();
            routeVoHandler.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
        }

        return resultPageInfo;
    }
}
